/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.syncinfo.FolderSyncInfo;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * ClientUser object for the 'p4 dirs' command.
  * 
  * @version $Revision: 67 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Dirs extends AbstractPerforceCommand implements IPerforceCommand
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the result of the operation. */
	private List result = new ArrayList();	
	
	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Convenience method for creating instances of Dirs.
	  *
	  * @param monitor for progress feedback.
	  * 
	  * @return the new Dirs instance.
	  */
	public static final Dirs newInstance(IProgressMonitor monitor)
	{
		MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null); 
		return new Dirs(status, monitor);
	}
	
	// ------------------------------------------------------------------------ Private Constructors
	
	/** Constructro for Dirs.
	  * 
	  * @param status the MultiStatus object.
	  * @param monitor for progress feedback.
	  */
	private Dirs(MultiStatus status, IProgressMonitor monitor)
	{		
		super(status, monitor);
	}

	// -------------------------------------------------------- Public Methods (IClientUserDelegate)
	
	/** @see IClientUserDelegate#outputStat(Map) */
	public void outputStat(Map strDict)
	{
		FolderSyncInfo syncInfo = new FolderSyncInfo();
		syncInfo.setDepotFileName((String) strDict.get("dir"));                        //$NON-NLS-1$
		
		result.add(syncInfo);
		
		getProgressMonitor().subTask(syncInfo.getDepotFileName());
		getProgressMonitor().worked(1);
	}
	
	// ----------------------------------------------------------- Public Methods (IPerforceCommand)
	
	/** @see IPerforceCommand#getCommand() */
	public String getCommand()
	{
		return "dirs";                                                                 //$NON-NLS-1$
	}
	
	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus()
	{
		return getMultiStatus();
	}
	
	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return false;
	}	

	// ------------------------------------------------------------------------------ Public Methods

	/** Returns the result of the operation. 
	  * 
	  * @return array of FolderSyncInfo.
	  */
	public FolderSyncInfo[] getResult()
	{
		return (FolderSyncInfo[]) result.toArray(new FolderSyncInfo[result.size()]);
	}
}
